<?php
/**
 * Created by IntelliJ IDEA.
 * User: admin
 * Date: 4/22 0022
 * Time: 11:57
 */

namespace app\index\model;


use think\Model;
use think\Session;

class Userapp extends Model
{

    /**
     * 购买一个APP记录
     * @param $userid
     * @return bool
     */
    public function buyapp($userid){
        $oriinfo=$this->where("appid",request()->post("appid"))->where(["shopid"=>Session::get("shopid")])->find();
        if($oriinfo){
            $expiretime=strtotime("+".request()->post("year")." year",time()>$oriinfo["endtime"]?time():$oriinfo["endtime"]);
            if(($expiretime>=pow(2,31)-1)||$expiretime<100){
                return false;
            }
            //        第一步:向Appbuyorder中记录
            $ordermodel=new Appbuyorder();
            $ordermodel->data([
                "shopid"=>Session::get("shopid"),
                "userid"=>$userid,
                "createtime"=>time(),
                "appid"=>request()->post("appid"),
                "appname"=>\model("applist")->where("id",request()->post("appid"))->find()["appname"],
                "expiretime"=>strtotime("+".request()->post("year")." year",time()>$oriinfo["endtime"]?time():$oriinfo["endtime"]),
                "orderstatus"=>1,
                "price"=>request()->post("price"),
                "num"=>request()->post("year"),
                "sn"=>uniqid(),
                "buyway"=>"应用市场在线支付"
            ]);
            $res1=$ordermodel->isUpdate(false)->save();
            //        第二步:向UserApp表中记录
            $res2=$this->isUpdate(true,["id"=>$oriinfo["id"]])->save([
                "appid"=>request()->post("appid"),
                "shopid"=>Session::get("shopid"),
                "status"=>1,
                "endtime"=>strtotime("+".request()->post("year")." year",time()>$oriinfo["endtime"]?time():$oriinfo["endtime"])
            ]);
        }else{
            //        第一步:向Appbuyorder中记录
            $ordermodel=new Appbuyorder();
            $ordermodel->data([
                "shopid"=>Session::get("shopid"),
                "userid"=>$userid,
                "createtime"=>time(),
                "appid"=>request()->post("appid"),
                "appname"=>\model("applist")->where("id",request()->post("appid"))->find()["appname"],
                "expiretime"=>strtotime("+".request()->post("year")." year",time()),
                "orderstatus"=>1,
                "price"=>request()->post("price"),
                "num"=>request()->post("year"),
                "sn"=>uniqid(),
                "buyway"=>"应用市场在线支付"
            ]);
            $res1=$ordermodel->save();
            //        第二步:向UserApp表中记录
            $this->data([
                "appid"=>request()->post("appid"),
                "shopid"=>Session::get("shopid"),
                "status"=>1,
                "starttime"=>time(),
                "endtime"=>strtotime("+".request()->post("year")." year",time())
            ]);
            $res2=$this->save();
        }

        return $res1&&$res2;//返回记录结果
    }

}